// Package coding_round_1 12_337
// @Title coding_round_1
// @Description
// @Author  == 琳琳也叫王木木 ==  2021/9/6 10:19
// @Create  2021/9/6 10:19
package coding_round_1

type RobInfo struct {
	Do    int
	NotDo int
}

func robProcess(root *TreeNode) RobInfo {
	if root == nil {
		return RobInfo{Do: 0, NotDo: 0}
	}
	do := root.Val // 偷x的最大受益
	noDo := 0      // 不偷x的最大受益
	if root.Left != nil {
		do += robProcess(root.Left).NotDo
		noDo += max(robProcess(root.Left).NotDo, robProcess(root.Left).Do)
	}
	if root.Right != nil {
		do += robProcess(root.Right).NotDo
		noDo += max(robProcess(root.Right).NotDo, robProcess(root.Right).Do)
	}
	return RobInfo{Do: do, NotDo: noDo}
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func RobIII(root *TreeNode) int {
	if root == nil {
		return 0
	}
	var ans int
	info := robProcess(root)
	ans = max(info.Do, info.NotDo)
	return ans
}
